﻿using Sustainalytics.ESG.Entities;
using Sustainalytics.Synchronization.Validation.ValidationRules.Comprehensive;
using System;
using Xunit;

namespace Sustainalytics.Synchronization.Validation.Tests
{
    [Trait("Category", "Unit")]
    public class IndicatorRawScoreChange
    {
        private readonly IndicatorRawScoreChangeRule _rule;

        public IndicatorRawScoreChange()
        {
            _rule = new IndicatorRawScoreChangeRule();
        }

        private RPIndicatorPerformance[] GetIndicators(string keysList, string valuesList)
        {
            var keys = keysList.Split(',');
            var values = valuesList.Split(',');

            var result = new RPIndicatorPerformance[keys.Length];

            for (var i = 0; i < keys.Length; i++)
                result[i] = new RPIndicatorPerformance() { Code = keys[i], RawScore = float.Parse(values[i]) };

            return result;
        }

        [Fact]
        public void Compare_IndicatorsRawScore_RuleApply()
        {
            var validated = new RPCompanyESGComputationResult()
            {
                Id = new Guid(),
                Indicators = GetIndicators("I1,I2,I3,I4,I5", "20,40,60,80,70")
            };

            var reference = new RPCompanyESGComputationResult()
            {
                Id = new Guid(),
                Indicators = GetIndicators("I1,I2,I3,I4,I5", "100,40,70,90,20"),
            };

            var result = _rule.IsValid(validated, reference);

            Assert.True(result);
        }

        [Fact]
        public void Compare_IndicatorsRawScore_RuleBreak()
        {
            var validated = new RPCompanyESGComputationResult()
            {
                Id = new Guid(),
                Indicators = GetIndicators("I1,I2,I3,I4,I5", "0,40,60,80,100")
            };

            var reference = new RPCompanyESGComputationResult()
            {
                Id = new Guid(),
                Indicators = GetIndicators("I1,I2,I3,I4,I5", "100,40,70,90,0"),
            };

            var result = _rule.IsValid(validated, reference);

            Assert.False(result);
        }
    }
}
